##  Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com 
##
##
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to the Free Software
##  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import os
import sys
import shutil
sys.path.append(os.path.join(os.getcwd(),'triangulum_dev','python'))

import pytriangulum
import pytriangulum.utils
from  pytriangulum.utils import inno

import VERSION
import Doxygen

def MKDIR(d):
    try:
        os.mkdir(d)
    except Exception, e:
        pass
        #print e

def build():
    try:
        os.remove('Output\\TriangulumBaseWin32_'+VERSION.getVersion()+'.exe')
    except Exception,e :
        pass
       # print e

   

    if os.path.isfile('Output\\setup.exe'):
        os.rename('Output\\setup.exe','Output\\TriangulumBaseWin32_'+VERSION.getVersion()+'.exe')
        try:  
            shutil.rmtree('release')
        except Exception,e :
            pass
        return False
    else:
        try:  
            shutil.rmtree('release')
        except Exception,e :
            pass
            #print e

        
        os.system('make.py lib')

        doc =  Doxygen.getDoc(VERSION.getProject(),VERSION.getVersion(),os.path.join(os.getcwd(),'release','include'),os.path.join(os.getcwd(),'release','docs'))  
        fh2 = open('Doxyfile','w')
        fh2.write(doc)
        fh2.close()
        os.system('"C:\\Program Files\\doxygen\\bin\\doxygen.exe" Doxyfile')

        shutil.copy('VERSION','release\\VERSION')
        shutil.copy('LICENSE','release\\LICENSE')
        shutil.copy('DEPENDENCIES','release\\DEPENDENCIES')
        shutil.copytree('resources','release\\resources')
        #shutil.copytree('VERSION','release\\VERSION')
        
        src_dir = 'release'
        dst_dir = '{pf}/Triangulum/Base'
        app = "TriangulumBaseWin32"
        version = VERSION.getVersion()
        output_iss = 'triangulumBaseBuild.iss'
        


        inno.genInnoScript(output_iss = output_iss,
                      src_dir = src_dir,
                      dst_dir = dst_dir,
                      app_name = app,
                      version = version)
    return True

def devSuite():
    src_dir = 'dev'
    dst_dir = '{pf}/Triangulum/Source/Base/VERSION_'+VERSION.getVersion()
    app = "TriangulumBaseSourceSuiteWin32"
    version = VERSION.getVersion()
    output_iss = 'triangulumBaseSource.iss'
    
    try:
        os.remove('Output\\TriangulumBaseSourceWin32_'+VERSION.getVersion()+'.exe')
    except Exception,e :
        pass
        #print e

   

    if os.path.isfile('Output\\setup.exe'):
        os.rename('Output\\setup.exe','Output\\TriangulumBaseSourceWin32_'+VERSION.getVersion()+'.exe')
        try:  
            shutil.rmtree('dev')
        except Exception,e :
            pass
        return False
    else:
        try:  
            shutil.rmtree('dev')
        except Exception,e :
            pass
            #print e
        MKDIR('dev')
        MKDIR('dev/src')
        MKDIR('dev/src/tinyxml')
        MKDIR('dev/src/sigslot')
        MKDIR('dev/tests')
        os.system('copy \"src\\*.h\" \"dev\\src\"')
        os.system('copy \"src\\*.cpp\" \"dev\\src\"')
        os.system('copy \"src\\sigslot\\*.h\" \"dev\\src\\sigslot\"')
        os.system('copy \"src\\tinyxml\\*.h\" \"dev\\src\\tinyxml\"')
        os.system('copy \"src\\tinyxml\\*.cpp\" \"dev\\src\\tinyxml\"')
        os.system('copy \"tests\\*.cpp\" \"dev\\tests\"')
        os.system('copy \"tests\\*.h\" \"dev\\tests\"')
        shutil.copy('VERSION','dev\\VERSION')
        shutil.copy('LICENSE','dev\\LICENSE')
        shutil.copy('Doxygen.py','dev\\Doxygen.py')
        shutil.copy('VERSION.py','dev\\VERSION.py')
        shutil.copy('make.py','dev\\make.py')
        shutil.copy('BUILD_SOURCE_PACK.py','dev\\BUILD_SOURCE_PACK.py')
        shutil.copy('BUILD_RELEASE.py','dev\\BUILD_RELEASE.py')
        shutil.copy('inno_script.py','dev\\inno_script.py')
        shutil.copy('DEPENDENCIES','dev\\DEPENDENCIES')
        shutil.copytree('resources','dev\\resources')
        shutil.copytree('tests/data','dev\\tests\\data')

        inno.genInnoScript(output_iss = output_iss,
                      src_dir = src_dir,
                      dst_dir = dst_dir,
                      app_name = app,
                      version = version)
    return True
#=============================================

##def main():
##   build()
##
###=============================================
##
##if __name__ == '__main__':
##    main()
